package cn.danger.com.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cn.bmob.v3.BmobUser;
import cn.danger.com.activity.R;
import cn.danger.com.model.entity.MyUser;

/**
 * Created by danger on 2016/4/6.
 */
public class FileCacheService {

    private Context context;

    public FileCacheService(Context context) {
        this.context = context;
    }

    /**
     * 保存文件
     *
     * @param fileName
     * @param map
     * @return
     */
    public boolean saveSharePreference(String fileName, Map<String, Object> map) {
        boolean flag = false;
        SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object object = entry.getValue();
            if (object instanceof Boolean) {
                Boolean b = (Boolean) object;
                editor.putBoolean(key, b);
            } else if (object instanceof Integer) {
                Integer integer = (Integer) object;
                editor.putInt(key, integer);
            } else if (object instanceof Float) {
                Float f = (Float) object;
                editor.putFloat(key, f);
            } else if (object instanceof Long) {
                Long l = (Long) object;
                editor.putLong(key, l);
            } else if (object instanceof String) {
                String str = (String) object;
                editor.putString(key, str);
            }
        }
        flag = editor.commit();
        return flag;
    }

    /**
     * 读取文件
     *
     * @param fileName
     * @return
     */
    public Map<String, ?> getSharePreference(String fileName) {
        Map<String, ?> map = null;
        SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        map = preferences.getAll();
        return map;
    }

    public void saveUserInfo(String phone, String password) {
        FileCacheService service = new FileCacheService(context);
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("password", password);
        map.put("photo", BmobUser.getCurrentUser(MyUser.class).getPhoto());
        map.put("email", BmobUser.getCurrentUser(MyUser.class).getEmail());
        map.put("nick_name", BmobUser.getCurrentUser(MyUser.class).getNick_name());
        service.saveSharePreference("userInfo", map);
    }

    public void deleteUserInfo() {
        SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        if (sp != null) {
            sp.edit().clear().commit();
        }
    }

    public void updatePhone(String phone) {
        FileCacheService service = new FileCacheService(context);
        Map<String, String> map = (Map<String, String>) service.getSharePreference("userInfo");
        String origin_password = map.get("password");
        String origin_photo = map.get("photo");
        String origin_email = map.get("email");
        String origin_nick_name = map.get("nick_name");
        Map<String, Object> new_map = new HashMap<>();
        new_map.put("phone", phone);
        new_map.put("password", origin_password);
        new_map.put("photo", origin_photo);
        new_map.put("email", origin_email);
        if (origin_nick_name.equals(map.get("phone"))) {
            new_map.put("nick_name", phone);
        } else {
            new_map.put("nick_name", origin_nick_name);
        }
        service.saveSharePreference("userInfo", new_map);
    }

    public void updatePassword(String password) {
        FileCacheService service = new FileCacheService(context);
        Map<String, Object> map = (Map<String, Object>) service.getSharePreference("userInfo");
        map.put("password", password);
        service.saveSharePreference("userInfo", map);
    }

    public void updatePhoto(String photo) {
        FileCacheService service = new FileCacheService(context);
        Map<String, Object> map = (Map<String, Object>) service.getSharePreference("userInfo");
        map.put("photo", photo);
        service.saveSharePreference("userInfo", map);
    }

    public void updateEmail(String email) {
        FileCacheService service = new FileCacheService(context);
        Map<String, Object> map = (Map<String, Object>) service.getSharePreference("userInfo");
        map.put("email", email);
        service.saveSharePreference("userInfo", map);
    }

    public void updateNickName(String nick_name) {
        FileCacheService service = new FileCacheService(context);
        Map<String, Object> map = (Map<String, Object>) service.getSharePreference("userInfo");
        map.put("nick_name", nick_name);
        service.saveSharePreference("userInfo", map);
    }

    public void defaultSetting() {
        FileCacheService service = new FileCacheService(context);
        Map<String, Object> map = new HashMap<>();
        map.put("show_finished_event", true);
        map.put("work_color", 1);
        map.put("route_color", 2);
        map.put("life_color", 3);
        map.put("auto_synchronous", true);
        service.saveSharePreference("setting", map);
    }
}
